package com.yangkunhong.service;

import com.yangkunhong.constant.ErrMsg;
import com.yangkunhong.dao.BaseDao;
import com.yangkunhong.dao.UserDao;
import com.yangkunhong.dao.UserLogDao;
import com.yangkunhong.pojo.User;
import com.yangkunhong.pojo.UserLog;
import com.yangkunhong.utils.DataCheckUtils;
import com.yangkunhong.utils.DateUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.Map;

public class UserService {

    public ErrMsg RegisterUser(Map<String,String> params){
        //判断用户名是否为空
        if(params.get("username").isEmpty()){
            return ErrMsg.USERNAME_ERR;
        }
        if(params.get("password").isEmpty()){
            return ErrMsg.PASSWORD_ERR;
        }
        if(params.get("re_password").isEmpty()){
            return ErrMsg.RE_PASSWORD_ERR;
        }
        //2次密码不匹配
        if(!params.get("password").equals(params.get("re_password"))){
            return ErrMsg.PASSWORD_MATCH_ERR;
        }
        //查询用户名是否已被占用
        User user = new User();
        user.setUsername(params.get("username"));
        User userByUsername = UserDao.findUserByUsername(user);
        //用户名已存在
        if(userByUsername != null ){
            return ErrMsg.USERNAME_EXISTS_ERR;
        }
        //插入数据库
        user.setPassword(params.get("password"));
        //1中文乱码  2需要动态数据
        user.setCreateAt(DateUtils.getLocalDateString()); //
        user.setCreateIp(params.get("ip"));
        //在Service不要写SQL
        boolean insert = UserDao.addRegisterUser(user);
        //注册失败
        if(!insert){
            return ErrMsg.REGISTER_ERR;
        }

        //注册日志

        UserLog userLog = new UserLog();
        userLog.setUserId(BaseDao.getLastInsertId().intValue());//当前注册的Id 自增
        //第一种：根据注册时的用户名，去查找对应的id【作业】
        //第二种：获取MySQL当前会话插入的自增Id
        userLog.setCreateIp(user.getCreateIp());//从注册信息中获取
        userLog.setCreateAt(user.getCreateAt());//从注册信息中获取
        userLog.setOp("reg");
        userLog.setUserAgent(params.get("userAgent"));//用户设备信息
        UserLogDao.addUserLog(userLog);
        //注册成功

        //事务操作【扩展要求】

        return ErrMsg.REGISTER_OK;


    }
    //处理登陆的核心逻辑
    public ErrMsg doLogin(Map<String,String> params, HttpServletRequest request) {
        //
        //1判断用户名是否为空
        if(params.get("username").isEmpty()){
            return ErrMsg.USERNAME_ERR;
        }
        if(params.get("password").isEmpty()){
            return ErrMsg.PASSWORD_ERR;
        }
        //2 根据用户名获取用户信息

        User user = new User();
        user.setUsername(params.get("username"));
        user.setPassword(params.get("password"));
        //找不到？？？ null
        User userByUsername = UserDao.findUserByUsername(user);
        if(userByUsername == null){
            return ErrMsg.USERNAME_NO_EXISTS_ERR;
        }
        //判断密码【密文】
        if(!userByUsername.getPassword().equals(user.getPassword())){
            return ErrMsg.PASSWORD_NO;
        }
        //更新用户信息 日活  周  月
        User updateInfo = new User();
        updateInfo.setLastAt(DateUtils.getLocalDateString());
        updateInfo.setLastIp(params.get("ip"));
        updateInfo.setId(userByUsername.getId());
        //更新失败
        if(UserDao.updateAfterLogin(updateInfo) <=0){
            return ErrMsg.ERROR;
        }

        //4.5 写入登录日志【作业】


        UserLog userLog = new UserLog();
        userLog.setUserId(userByUsername.getId());//当前登录用户的ID
        userLog.setCreateIp(updateInfo.getLastIp());//从注册信息中获取
        userLog.setCreateAt(updateInfo.getLastAt());//从注册信息中获取
        userLog.setOp("login");
        userLog.setUserAgent(params.get("userAgent"));//用户设备信息
        UserLogDao.addUserLog(userLog);


        //4.6  写入session
        HttpSession httpSession = request.getSession();
        userByUsername.setLastIp(updateInfo.getLastIp());
        userByUsername.setLastAt(updateInfo.getLastAt());
        httpSession.setAttribute("USER_INFO",userByUsername);

        return ErrMsg.LOGIN_OK;

    }

    public ErrMsg doUpdatePassword(User user, Map<String, String> map,HttpServletRequest req) {
        //修改密码的逻辑
        //{oldPassword=1111, newPassword=111111, rePassword=111111}
        System.out.println(map);
        if(DataCheckUtils.isEmpty(map.get("oldPassword")) ){
            return ErrMsg.PASSWORD_ERR;//请输入旧密码
        }
        if(DataCheckUtils.isEmpty(map.get("newPassword"))){
            return ErrMsg.NEW_PASSWORD_ERR;//请输入新密码
        }
        if(DataCheckUtils.isEmpty(map.get("rePassword"))){
            return ErrMsg.RE_PASSWORD_ERR;//请再次输入新密码
        }
        if(!map.get("rePassword").equals(map.get("newPassword"))){
            return ErrMsg.PASSWORD_MATCH_ERR;//密码不一致
        }
        //验证旧密码是否正确
        if(!user.getPassword().equals(map.get("oldPassword"))){
            System.out.println("oldPassword:"+map.get("oldPassword"));
            return ErrMsg.OLD_PASSWORD_ERR;//旧密码错误
        }

        //更新密码
        user.setPassword(map.get("newPassword"));
        if(UserDao.updatePassword(user) <=0){
            return ErrMsg.PASSWORD_ERR; //修改失败
        }
        //写入修改日志
        //绑定数据
        UserLog userLog = new UserLog();
        userLog.setUserId(user.getId());
        userLog.setCreateAt(DateUtils.getLocalDateString());
        userLog.setCreateIp(user.getLastIp());//
        userLog.setOp("password");
        userLog.setUserAgent(req.getHeader("User-Agent"));
        //写入
        UserLogDao.addUserLog(userLog);
        //清除session
        req.getSession().removeAttribute("USER_INFO");
        return ErrMsg.PASSWORD_OK;//操作成功
    }
}
